# -*- coding: utf-8 -*-
"""
数据清洗模块
负责门店数据的清洗、标准化和修正处理
"""
import pandas as pd
import numpy as np
import re
from config_utils import load_and_validate_config
CONFIG = load_and_validate_config()

def correct_zhanqu(df_mendian):
    print("已解约门店修正战区信息")
    """
    修正战区信息
    处理已解约门店的战区经理信息，并根据区域信息匹配合适的管理人员
    :param df_mendian: 门店数据DataFrame
    :return: 修正后的DataFrame
    """
    # 已解约门店清空相关经理信息
    condition = df_mendian['运营状态'].str.contains('已解约', na=False, regex=False)
    df_mendian.loc[condition, ['大区经理', '省区经理', '区域经理', '收银机ID']] = np.nan
    
    # 读取修正数据
    df_correct = pd.read_excel(CONFIG['correct_file'])
    df_correct = df_correct.replace('ALL', np.nan)
    mendian_columns = df_mendian.columns.tolist()
    
    # 按区匹配
    df_qu = df_correct[df_correct['区'].notna()]
    df_mendian = pd.merge(df_mendian, df_qu, on=['省','市','区'], how='left', suffixes=['','_匹'])
    mask = df_mendian["大区经理"].isna()
    df_mendian.loc[mask, "大区经理"] = df_mendian.loc[mask, "大区经理_匹"]
    df_mendian.loc[mask, "省区经理"] = df_mendian.loc[mask, "省区经理_匹"]
    df_mendian.loc[mask, "南北战区"] = df_mendian.loc[mask, "南北战区_匹"]
    df_mendian = df_mendian.loc[:, mendian_columns]
    
    # 按市匹配
    df_shi = df_correct[(df_correct['市'].notna()) & (df_correct['区'].isna())]
    df_mendian = pd.merge(df_mendian, df_shi, on=['省','市'], how='left', suffixes=['','_匹'])
    mask = df_mendian["大区经理"].isna()
    df_mendian.loc[mask, "大区经理"] = df_mendian.loc[mask, "大区经理_匹"]
    df_mendian.loc[mask, "省区经理"] = df_mendian.loc[mask, "省区经理_匹"]
    df_mendian.loc[mask, "南北战区"] = df_mendian.loc[mask, "南北战区_匹"]
    df_mendian = df_mendian.loc[:, mendian_columns]
    
    # 按省匹配
    df_sheng = df_correct[(df_correct['市'].isna()) & (df_correct['区'].isna())]
    df_mendian = pd.merge(df_mendian, df_sheng, on=['省'], how='left', suffixes=['','_匹'])
    mask = df_mendian["大区经理"].isna()
    df_mendian.loc[mask, "大区经理"] = df_mendian.loc[mask, "大区经理_匹"]
    df_mendian.loc[mask, "省区经理"] = df_mendian.loc[mask, "省区经理_匹"]
    df_mendian.loc[mask, "南北战区"] = df_mendian.loc[mask, "南北战区_匹"]
    
    # 区域经理修改为代管
    df_mendian['区域经理'] = df_mendian['区域经理'].fillna(df_mendian['省区经理'])
    
    # 直营店修复
    df_mendian.loc[df_mendian['门店编号'].str.startswith('ZYD', na=False), 
                  ['大区经理', '省区经理', '区域经理','南北战区']] = '直营店'
    
    return df_mendian.loc[:, mendian_columns]


def store_data_cleaner(df, invalid_keywords=['测试','茶语','茶讯','柬埔寨'],
                      include_prefixes=['TLL', 'ZYD'], special_prefix='ZYD'):
    print('门店数据清洗:')
    """
    智能门店数据清洗函数
    执行数据清洗、过滤无效记录、标准化字段格式等操作
    
    参数:
    df -- 原始数据框 (pd.DataFrame)
    invalid_keywords -- 需要过滤的名称关键词 (list)
    include_prefixes -- 需要保留的门店编号前缀 (list)
    special_prefix -- 特殊门店编号前缀 (str)
    
    返回:
    清洗后的数据框 (pd.DataFrame)
    """
    # 创建副本避免修改原始数据
    df_clean = df.copy()
    # 第一阶段：基础清洗
    df_clean = (
        df_clean
        .pipe(_handle_missing_data)
        .pipe(_filter_invalid_records, invalid_keywords, include_prefixes)
        .pipe(_process_special_stores, special_prefix)
    )
    
    # 第二阶段：字段标准化
    df_clean = (
        df_clean
        .pipe(_clean_manager_columns)
        .pipe(_convert_data_types)
    )
    
    # 应用战区信息修正
    df_clean = correct_zhanqu(df_clean)
    
    # 合并门店标识信息
    # 局部导入解决循环依赖
    from db_utils import read_store_sign_from_db
    df_sign = read_store_sign_from_db()
    df_clean = pd.merge(df_clean, df_sign, how='left', on='门店编号')
    df_clean['新店老店'] = df_clean['新店老店'].fillna('新店')
    
    return df_clean


# ================ 内部辅助函数 ================

def _handle_missing_data(df):
    """处理缺失值，删除门店编号为空的记录并清理门店名称空格"""
    # 清除门店名称前后空格
    df['门店名称'] = df['门店名称'].str.strip()
    print('删除门店编号为空的记录')
    return df.dropna(subset=['门店编号'])


def _filter_invalid_records(df, keywords, include_prefixes):
    print('仅保留甜啦啦门店')
    """过滤无效记录：排除包含指定关键词的门店和指定前缀的门店编号"""
    pattern = '|'.join(keywords)
    mask = (
        ~df['门店名称'].str.contains(pattern, na=False) &
        df['门店编号'].str.startswith(tuple(include_prefixes), na=False)
    )
    return df[mask]


def _process_special_stores(df, prefix):
    print('直营店管理信息填充')
    """处理特殊门店：为指定前缀的门店设置默认管理人员信息"""
    mask = df['门店编号'].str.startswith(prefix, na=False)
    df.loc[mask, ['大区经理', '省区经理', '区域经理','南北战区']] = '直营店'
    return df

def _clean_manager_columns(df):
    print('补充代管门店信息')
    """清洗管理字段：删除括号及内容，处理空值"""
    # 填充空值
    df['区域经理'] = df['区域经理'].fillna(df['省区经理'])
    
    print('清除经理姓名中的括号')
    pattern = re.compile(r'[（(][^）)]*[）)]')
    
    # 清洗指定列
    for col in ['大区经理', '省区经理', '区域经理']:
        df[col] = df[col].astype(str).str.replace(pattern, '', regex=True)
    return df


def _convert_data_types(df):
    print('u8c客商编码转换为字符串')
    """转换数据类型：确保特定字段使用正确的数据类型"""
    if 'U8C客商编码' in df.columns:
        df['U8C客商编码'] = df['U8C客商编码'].astype(str)
    return df
